package com.bl.student.dao.redis;

import com.bl.student.dao.redis.model.TeacherInfo;
import com.google.common.base.Optional;
import lombok.extern.log4j.Log4j2;
import org.springframework.stereotype.Service;

/**
 * Created by yujingyi on 2018/2/25.
 */
@Service
@Log4j2
public class TeacherInfoDao extends AbstractDao {
    private final String TEACHER_LOGIN_KEY_PREFIX = "LOGIN_TEACHER_";

    private final long TEACHER_LOGIN_KEY_EXPIRE = 60 * 60 * 24 * 30;


    public Optional<TeacherInfo> getUserInfo(String mobile) {

        TeacherInfo authInfo = hgetAll(genKey(TEACHER_LOGIN_KEY_PREFIX, mobile), TeacherInfo.class);

        return authInfo == null ?
                Optional.<TeacherInfo>absent() :
                Optional.fromNullable(authInfo);
    }

    public void setUserInfo(TeacherInfo authInfo) {
        String key = genKey(TEACHER_LOGIN_KEY_PREFIX, authInfo.getAccount());
        hsetAll(key, authInfo);
        expire(key, TEACHER_LOGIN_KEY_EXPIRE);
    }

    public void setUserInfoAttribute(String account, String key, String value) {

        hset(genKey(TEACHER_LOGIN_KEY_PREFIX, account), key, value);

    }

    public boolean delUserInfo(String account) {

        return del(genKey(TEACHER_LOGIN_KEY_PREFIX, account)) > 0 ? true : false;
    }

}
